package com.cip.master.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.velocity.app.VelocityEngine;

import com.cip.common.util.CommonUtil;
import com.cip.common.util.GridUtil;
import com.cip.master.dao.SubjectDAO;
import com.cip.master.model.Subject;
import com.cip.master.service.SubjectService;

public class SubjectServiceImpl implements SubjectService{
	
	private SubjectDAO subjectDAO;
	private VelocityEngine velocityEngine;
	
	public String getSubjectGridList(Locale locale)throws Exception {
		
		String gridSource = ""; 
		
		try {
			List<Subject> subjectList = getAllSubjectList();
			
			List<Map<String,Object>> subjectArrayList = new ArrayList<Map<String,Object>>();
			
			for (Subject subject : subjectList) {
				Map<String,Object> subjectObj = new HashMap<String, Object>();
				subjectObj.put("id", "<a href='/editSubject?method=showEditSubject&subject.id="+subject.getId()+"' style='text-decoration:underline;color:blue;'>"+subject.getCode()+"</a>");
				subjectObj.put("classYearName", subject.getClassYear().getName());
				subjectObj.put("subject", subject.getSubjectName());
				subjectObj.put("status", subject.getStatus());
				
				subjectArrayList.add(subjectObj);			
			}
			
			gridSource = generateScriptForProcess(velocityEngine, subjectArrayList, locale);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return gridSource;
	}
	
	/**
	 * Set into context of grid column names and field names and it attributes
	 * 
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public static String generateScriptForProcess(VelocityEngine velocityEngine, List<Map<String, Object>> subjectArrayList, Locale locale) throws Exception {
		
		return GridUtil.generateScriptForProcess(velocityEngine, subjectArrayList, createSubjectGridColumnMapping(locale));
	}
	
	/**
	 * Add needed column and it mapping for my instances
	 * 
	 * @param context
	 */
	private static Map<String, Object> createSubjectGridColumnMapping(Locale locale) {
		Map<String, Object> context = new HashMap<String, Object>();
		List<Map<String, Object>> fieldNameList = new ArrayList<Map<String, Object>>();
		//ResourceBundle appResourceBundle = ResourceBundle.getBundle(Constants.BUNDLE_KEY ,locale);
		//context.put("title", appResourceBundle.getString("process.myInstances"));
		context.put("title", "Subject List");
		context.put("gridId", "MY_SUBJECTLIST");
		context.put("needTreeStructure", true);
		//context.put("dynamicGridWidth", "300px");
		//context.put("dynamicGridHeight", "580px");
		String columnNames = "['Subject Code','Class Name','Subject','Status']";
        CommonUtil.createFieldNameList(fieldNameList, "id", "100", "center", "","false");
        CommonUtil.createFieldNameList(fieldNameList, "classYearName", "100", "left","", "false");
        CommonUtil.createFieldNameList(fieldNameList, "subject", "100", "left","", "false");
        CommonUtil.createFieldNameList(fieldNameList, "status", "100", "center", "", "false");

        context.put("columnNames", columnNames);
        context.put("noOfRecords", "10");
		context.put("fieldNameList", fieldNameList);
		context.put("gridHeading", "true");
		
		return context;
	}
	
	public List<Subject> getAllSubjectList() throws Exception {
		return subjectDAO.getAllSubjectList();
	}

	public Subject getSubjectById(int id) throws Exception {
		return subjectDAO.getSubjectById(id);
	}

	public Subject saveOrUpdateSubject(Subject subject) throws Exception {
		return subjectDAO.saveOrUpdateSubject(subject);
	}

	public Subject editSubject(Subject subject) throws Exception {
		return subjectDAO.editSubject(subject);
	}
	
	public Subject getSubjectByCode(String code) throws Exception{
		return subjectDAO.getSubjectByCode(code);
	}
	
	public Subject getSubjectByName(String subjectName) throws Exception{
	 return subjectDAO.getSubjectByName(subjectName);
	}

	public SubjectDAO getSubjectDAO() {
		return subjectDAO;
	}

	public void setSubjectDAO(SubjectDAO subjectDAO) {
		this.subjectDAO = subjectDAO;
	}

	public VelocityEngine getVelocityEngine() {
		return velocityEngine;
	}

	public void setVelocityEngine(VelocityEngine velocityEngine) {
		this.velocityEngine = velocityEngine;
	}

}
